﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Code4WP7SDK;
using System.Collections.ObjectModel;
using LuWeibo.Models;
using LuWeiboSDK;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Diagnostics;

namespace LuWeibo.ViewModels
{
    public class LuHomePageViewModel : BaseModel
    {
        
        #region Fields
        private ObservableCollection<Status> _showStatus;
        private ICommand _showWeiboCommand;
        private ICommand _searchUserCommand;
        #endregion

        #region Constructor
        public LuHomePageViewModel()
        {

        }
        #endregion

        #region Properties
        public ObservableCollection<Status> ShowStatus
        {
            get
            {
                if (this._showStatus == null)
                {
                    this._showStatus = new ObservableCollection<Status>();
                }
                return _showStatus;
            }
            set
            {
                _showStatus = value;
                this.NotifyPropertyChanged(p => p.ShowStatus);
            }
        }
        #endregion

        #region Command
        public ICommand ShowWeiboCommand
        {
            get
            {
                if (this._showWeiboCommand == null)
                {
                    this._showWeiboCommand = new BaseCommand(ShowWeiboExecute);
                }
                return _showWeiboCommand;
            }
        }
        public ICommand SearchUserCommand
        {
            get
            {
                if (this._searchUserCommand == null)
                {
                    this._searchUserCommand = new BaseCommand(SearchUser);
                }
                return _searchUserCommand;
            }
        }
        #endregion

        #region Method
        public void ShowWeiboExecute()
        {
            //组合参数
            List<Parameter> paras = new List<Parameter>();
            string data = ParameterHelper.BuildParameters(paras);
            //发送请求
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
                {
                    Debug.WriteLine("str:" + e.Result.ToString());
                    WeiboModel weiboModel = JsonConvert.DeserializeObject<WeiboModel>(e.Result.ToString());
                    this.ShowStatus = weiboModel.statuses;
                };
            ws.GetRequest(Urls.WeiboUrl, data);
            //接受数据
        }

        public void SearchUser()
        {
            List<Parameter> paras = new List<Parameter>();
            paras.Add(new Parameter("screen_name", "啊酷啦嘛它它"));
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        Debug.WriteLine(e.Result.ToString());
                    }
                    else
                    {
                        //出错了
                    }
                    
                };
            ws.GetRequest(Urls.Users_show, data);
        }
        #endregion
    }
}
